package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;

import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.entity.AccountRecordEntity;
import yf.mall.entity.LevelEntity;
import yf.mall.enums.MallOrderBusTypeEnum;
import yf.mall.service.MallStoreService;
import yf.mall.service.MyService;
import yf.mall.vo.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaolongtongxie
 * @version 1.0
 * @date 2020/6/11 18:10
 */
@RequestMapping("/api/my")
@RestController
public class ApiMyController {

    @Autowired
    private MyService myService;
    @Autowired
    private MallStoreService storeService;

    /**
     * Long
     * 订单接口
     *
     * @param userId
     * @param orderStatus
     * @param payStatus
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/myOrderList")
    public R orderList(@LoginUserId Long userId, Integer orderStatus, Integer payStatus, int page, int limit) {
        List<MallOrderVo> orderVos = this.myService.myOrderList(userId, orderStatus, payStatus, page, limit);
        return R.ok(orderVos);
    }

/*
    @RequestMapping("/myOrderInfo")
    public R myOrderInfo(@LoginUserId Long userId) {
        OrderMyInfo orderMyInfo = this.myService.findOrderStatus(userId);
        return R.ok(orderMyInfo);
    }
*/
    /** que
     *  新的发货转货库存列表
     * @param userId
     * @return
     */
    @RequestMapping("/store/list")
    public R storeList(@LoginUserId Long uid,Long userId){
        if (userId == null) {
            userId = uid;
        }
        List<StoreTypeVo> storeTypeVOS = storeService.storeType(userId);
        return R.ok(storeTypeVOS);
    }



    /** que
     * 我的利润
     * @param userId
     * @return
     */
    @RequestMapping("/myProfits")
    public R myProfits(@LoginUserId Long userId){
        AccountMyProfitVO myProfit = myService.findByUserIdAndBus(userId);
        return R.ok(myProfit);
    }

    /** que
     * 我的利润明细
     * @param userId
     * @return
     */
    @RequestMapping("/myProfitsInfo")
    public R myProfitsInfo(@LoginUserId Long userId,Integer bus,int page,int limit){
        PageHelper.startPage(page,limit);
        List<AccountRecordEntity> myProfit = myService.myProfitsInfo(userId,bus);
        return R.ok(myProfit);
    }

    /** que
     *  根据订单编号递归查询
     * @param
     * @return
     */
    @RequestMapping("/findByInfo")
    @IgnoreAuth
    public R findByInfo(String info){
        List<String> res = findByInfo(info,true);
        return R.ok(res);
    }
    /** que
     *   扣进货金额
     * @param
     * @return
     */
    @RequestMapping("/recursionOrderNo")
    @IgnoreAuth
    public R recursionOrderNo(String info){
        List<String> res = findByInfo(info,false);
        //收款详情页获取扣进货金额
        BigDecimal price = new BigDecimal(0);
        if (res != null && res.size() > 0){
            for (String str : res) {
               if (str.contains(MallOrderBusTypeEnum.AUTO_BUY.getValue())){
                   String s = str.split(MallOrderBusTypeEnum.AUTO_BUY.getValue())[1];
                   if (s != null){
                       price = new BigDecimal(s);
                   }
                   break;
               }
           }
        }
        return R.ok(price);
    }

    //根据info来查询 获取订单带来的连锁奖励or扣进货金额  que
    private List<String> findByInfo(String info,boolean flag) {
        List<String> res = new ArrayList<>();
        if (info != null && !("").equals(info)){
            if (info.contains(Const.ORDER_NO)){
                String orderNo = info.split(Const.ORDER_NO)[1];
                if (flag){
                    res = myService.findOrderNo(orderNo,res);
                }else {
                    res = myService.recursionOrderNo(orderNo,res);
                }
            }else {
                if (flag){
                    res = myService.findOrderNo(info,res);
                }else {
                    res = myService.recursionOrderNo(info,res);
                }
            }
            if (info.contains("下单支付")){
                String orderNo = info.split("下单支付")[1];
                if (flag){
                    res = myService.findOrderNo(orderNo,res);
                }else {
                    res = myService.recursionOrderNo(orderNo,res);
                }
            }
        }
        return res;
    }
}
